package com.fita.admin.web.controller.coupon;

import com.demo2do.core.entity.page.Page;
import com.demo2do.core.entity.result.HttpResult;
import com.demo2do.core.utils.JsonUtils;
import com.demo2do.core.web.interceptor.MenuSetting;
import com.demo2do.core.web.utils.WebUtils;
import com.fita.admin.entity.content.CouponBatchSearch;
import com.fita.admin.service.DataService;
import com.fita.admin.service.coupon.CouponService;
import com.fita.entity.coupon.Coupon;
import com.fita.entity.coupon.CouponBatch;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.beans.factory.annotation.Qualifier;
import org.springframework.http.MediaType;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.*;
import org.springframework.web.servlet.ModelAndView;

import javax.servlet.http.HttpServletRequest;
import java.util.List;

/**
 * 优惠券批次
 * 
 * @author LZJ
 *
 */
@Controller
@RequestMapping(path = "/coupon-batch")
@MenuSetting("menu-coupon")
public class CouponBatchController {
	
	
	 @Autowired
	 private CouponService<CouponBatch> couponBatchService;
	    
	 @Autowired
	 @Qualifier("dataService")
	 private DataService<Coupon> couponService;

	 private static final Logger logger = LoggerFactory.getLogger(CouponBatchController.class);
	 
    /**
     * @param request 
     * @return
     */
    @GetMapping(path = "")
    @MenuSetting("submenu-coupon-batch-management")
    public ModelAndView list(HttpServletRequest request, Page page) {
        
        ModelAndView modelAndView = new ModelAndView("coupon/coupon-batch/coupon-batches-list");
        
        List<CouponBatch> couponBatches = couponBatchService.listCouponBatches(page);
        
        modelAndView.addObject("couponBatches", couponBatches);
        modelAndView.addObject("refer", "list");        
        
        modelAndView.addObject("queryString", WebUtils.extractQueryString(request, "filter", "page"));
        
        return modelAndView;
    }
    
    
    /**
     * @param batchId
     * @return
     */
    @GetMapping(path = "/{batchId}")
    public ModelAndView view(HttpServletRequest request,@PathVariable Long batchId, Page page) {
        
        ModelAndView modelAndView = new ModelAndView("coupon/coupon-batch/coupon-batch-view");       
        
        CouponBatch couponBatch = couponBatchService.load(CouponBatch.class, batchId);
        
        int totals =  couponBatchService.countCoupon(couponBatch);
        if(totals >0) {
        	
        	page.with(totals);
        	page.calculate();
        	
        	List<Coupon> coupons = couponBatchService.listCoupons(couponBatch, page);
        	 
            modelAndView.addObject("coupons", coupons);
        }
        
       
        
        modelAndView.addObject("couponBatch", couponBatch);
        modelAndView.addObject("page", page);
        
       // modelAndView.addObject("queryString", WebUtils.extractQueryString(request, "filter", "page"));
        
        return modelAndView;
    }
    
    
    /**
     * @return
     */
    @GetMapping(path = "/add")
    public ModelAndView add() {
        
        ModelAndView modelAndView = new ModelAndView("coupon/coupon-batch/coupon-batch-add");
        
        return modelAndView;
    }
    
    
    /**
     * Do create coupon batch
     * 
     * @param couponBatch 
     * @return
     */
    @RequestMapping( value = "/create", method = RequestMethod.POST, produces=MediaType.APPLICATION_JSON_UTF8_VALUE)
    public @ResponseBody String onCreate(CouponBatch couponBatch) {
        
        HttpResult result = new HttpResult();
        
        try {
            
            couponBatchService.createCouponBatch(couponBatch);
            result.success().message("新建优惠券批次成功！");
        
        } catch(Exception e) {
            result.fail("500").message("新建优惠券批次失败！");
            
        }
        
        return JsonUtils.toJson(result);
    }
    
    /**
     * 
     * @param id
     * @return
     */
    @RequestMapping(value = "/{id}/generate-coupon", method = RequestMethod.GET)
    public ModelAndView generateCoupon(@PathVariable Long id){
        
        ModelAndView modelAndView = new ModelAndView("coupon/coupon-batch/coupon-generate");
        
        CouponBatch couponBatch = couponBatchService.load(CouponBatch.class, id);
        
        modelAndView.addObject("couponBatch", couponBatch);
        
        return modelAndView;
        
    }
    
    /**
     * 
     * @param id
     * @return
     */
    @RequestMapping(value = "/{id}/generate-coupon", method = RequestMethod.PATCH)
    public @ResponseBody String onGenerateCoupon(@PathVariable("id") Long id){
        
    	HttpResult result = new HttpResult();
        
        try {
            
            CouponBatch couponBatch = couponBatchService.load(CouponBatch.class, id);
            
            couponBatchService.generateCoupon(couponBatch);

            result.success().message("预生成券成功！");

        } catch (Exception e) {

            result.fail("500").message("预生成券失败！");

        }
        
        return JsonUtils.toJson(result);
    }
    
    
    /**
     * Display the search panel
     * 
     * @return
     */
    @RequestMapping(value = "/search", method = RequestMethod.GET)
    public ModelAndView search() {
        
        ModelAndView modelAndView = new ModelAndView("coupon/coupon-batch/coupon-batch-search");
        
        return modelAndView;
    }
    
    /**
     * Do search
     * 
     * @param couponBatchSearch
     * @param request
     * @param page
     * @return
     */
    @RequestMapping(value = "/search", method = RequestMethod.GET, params = "filter")
    @MenuSetting("submenu-coupon-batch-list")
    public ModelAndView onSearch(CouponBatchSearch couponBatchSearch, HttpServletRequest request, Page page) {
        
        ModelAndView modelAndView  = new ModelAndView("coupon/coupon-batch/coupon-batches-list");
        
        List<CouponBatch> couponBatches = couponBatchService.searchCouponBatch(couponBatchSearch, page);
        
        modelAndView.addObject("couponBatches", couponBatches);
        modelAndView.addObject("refer", "search");
        modelAndView.addObject("queryString", WebUtils.extractQueryString(request, "page", "filter"));
        
        return modelAndView;
        
    }
    
    
    /**
     * 
     * @param id 
     * @param couponId
     * @return
     */
    @RequestMapping(value = "/{id}/coupons/{couponId}/identify", method = RequestMethod.GET)
    public ModelAndView identifyCoupon(@PathVariable("id") Long id,
                                       @PathVariable("couponId") Long couponId){
        
        ModelAndView modelAndView = new ModelAndView("coupon/coupon-batch/coupon-identify");
        
        modelAndView.addObject("id", id);
        
        Coupon coupon = couponService.load(Coupon.class, couponId);
        modelAndView.addObject("coupon", coupon);
        
        return modelAndView;
    }
    
    /**
     * 
     * @param couponId
     * @param identification
     * @return
     */
    @RequestMapping(value = "/{id}/coupons/{couponId}/identify", method = RequestMethod.PATCH)
    public @ResponseBody String onIdentifyCoupon(@PathVariable("couponId") Long couponId,
                                                 @RequestParam("identification") String identification){
        
        HttpResult result = new HttpResult();
        
        try {
            
            Coupon coupon = couponBatchService.loadCoupon(couponId);
            
            System.out.println("优惠券id  : "+coupon.getId());
            
            couponBatchService.identify(coupon, identification);

            result.success().message("优惠券指定用户成功！");

        } catch (Exception e) {

            result.fail("500").message("优惠券指定用户失败！");

        }
        return JsonUtils.toJson(result);
    }
   
}
